package cmd

import (
	"gitee.com/huoyu123hk/cloudAgent/pkg/appManager"
	"github.com/spf13/cobra"
)

var (
	token   string
	project string
	path    string
	name    string
	version string
)

// appManagerCmd represents the appManager command
var appManagerCmd = &cobra.Command{
	Use:   "appManager",
	Short: "Manage application files",
	Long:  `Download, upload and manage application files`,
}

// appManagerDownloadCmd represents the download command
var appManagerDownloadCmd = &cobra.Command{
	Use:   "download [project]",
	Short: "Download application files",
	Long:  `Download the latest version of application files`,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		appManager.DownloadApp(token, serverAddr, project)
	},
}

// appManagerUploadCmd represents the upload command
var appManagerUploadCmd = &cobra.Command{
	Use:   "upload [path]",
	Short: "Upload a file",
	Long:  `Upload a file to the server`,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		path = args[0]
		appManager.UploadFile(token, serverAddr, path)
	},
}

var appManagerDeployCmd = &cobra.Command{
	Use:   "deploy [project]",
	Short: "Deploy application files",
	Long:  `Deploy the latest version of application files`,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		deployDir := args[0]
		if deployDir == "" {
			deployDir = "/data/cfs/" + name
		}
		BackupDir = args[1]
		appManager.DeployApp(token, serverAddr, project, name, version)
	},
}

func init() {
	// 添加appManager命令到根命令
	rootCmd.AddCommand(appManagerCmd)

	// 添加子命令
	appManagerCmd.AddCommand(appManagerDownloadCmd)
	appManagerCmd.AddCommand(appManagerUploadCmd)
	appManagerCmd.AddCommand(appManagerDeployCmd)

	// 添加标志
	appManagerDownloadCmd.Flags().StringVarP(&token, "token", "t", "36a0fe509897f0d4f3fa8546985e9878", "Authentication token")
	appManagerDownloadCmd.Flags().StringVarP(&project, "project", "p", "uat", "project code")
	appManagerUploadCmd.Flags().StringVarP(&token, "token", "t", "36a0fe509897f0d4f3fa8546985e9878", "Authentication token")
	appManagerDeployCmd.Flags().StringVarP(&token, "token", "t", "36a0fe509897f0d4f3fa8546985e9878", "Authentication token")
	appManagerDeployCmd.Flags().StringVarP(&project, "project", "p", "uat", "project code")
	appManagerDeployCmd.Flags().StringVarP(&name, "name", "n", "cfs-system", "application name")
	appManagerDeployCmd.Flags().StringVarP(&version, "version", "v", "1.0.0", "application version")
}
